package com.yd.tzkk.web;

import android.webkit.MimeTypeMap;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yd.tzkk.App;

import java.io.InputStream;

public class MyWebViewClient extends WebViewClient {

//        @Override
//        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//            handler.proceed(); // 允许明文传输
//        }


//    @Override
//    public boolean shouldOverrideUrlLoading(WebView view, String url) {
//        return super.shouldOverrideUrlLoading(view, url);
//    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        String url = request.getUrl().toString();
        if(!url.startsWith(App.MAIN_URL)){
            return super.shouldInterceptRequest(view, request);
        }
        String path = "www/dist"+removeDomain(url);
        // 加载assets资源
        try {
            String mimeType = getMimeType(url);
            if(mimeType!=null){
                InputStream inputStream = view.getContext().getAssets().open(path);
                return new WebResourceResponse(mimeType, "UTF-8", inputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.shouldInterceptRequest(view, request);
    }
    private String getMimeType(String url) {
        String mimeType = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {
            mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
        return mimeType;
    }
    private String removeDomain(String url) {
        url = url.replace("//","");
        int startOfPath = url.indexOf("/");
        if (startOfPath >10) {
            return url.substring(startOfPath);
        }
        return url;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }

//    @Override
//    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//        // 处理错误页面
//        // 可以在这里显示自定义的错误页面，或者进行其他处理逻辑
//        view.loadUrl("file:///www/error.html");
//    }
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        // 处理错误页面
        // 可以在这里显示自定义的错误页面，或者进行其他处理逻辑
//        view.loadUrl("file:///android_asset/www/error.html");
        super.onReceivedError(view,request,error);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
    }

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
//        view.loadUrl("file:///android_asset/www/error.html");
        super.onReceivedHttpError(view, request, errorResponse);
    }

}
